package com.qzdsoft.erpcloud.service.sale.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.qzdsoft.erpcloud.domain.sale.SaleQuotation;
import com.qzdsoft.erpcloud.mapper.sale.SaleQuotationDetailMapper;
import com.qzdsoft.erpcloud.mapper.sale.SaleQuotationMapper;
import com.qzdsoft.erpcloud.mapper.sale.vi.SaleQuotationViEnMapper;
import com.qzdsoft.erpcloud.service.sale.SaleQuotationService;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationEditEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationQueryCriteriaViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationViEn;
import com.qzdsoft.utils.DateUtil;
import com.qzdsoft.utils.entityUtil.UtilsEntity;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.sale.SaleQuotationStatus;

@Service
public class SaleQuotationServiceImpl implements SaleQuotationService {

	private static final Logger logger = LoggerFactory.getLogger(SaleQuotationServiceImpl.class);

	@Autowired
	private SaleQuotationViEnMapper saleQuotationViEnMapper;

	@Autowired
	private SaleQuotationDetailMapper saleQuotationDetailMapper;

	@Autowired
	private SaleQuotationMapper saleQuotationMapper;

	@Override
	public LayTableResponse<SaleQuotationViEn> findSaleQuotationsBy(SaleQuotationQueryCriteriaViEn saleQuotationQueryCriteriaViEn) {
		logger.debug("查询销售报价单，参数：{}", saleQuotationQueryCriteriaViEn);
		// 指定时间+1天
		if (!StringUtil.isEmpty(saleQuotationQueryCriteriaViEn.getEndDate())) {
			saleQuotationQueryCriteriaViEn
					.setEndDate(DateUtil.getSpecifiedDayAfter(saleQuotationQueryCriteriaViEn.getEndDate()));
		}
		PageHelper.startPage(saleQuotationQueryCriteriaViEn.getPage(), saleQuotationQueryCriteriaViEn.getLimit());
		List<SaleQuotationViEn> orderList = saleQuotationViEnMapper.selectAll(saleQuotationQueryCriteriaViEn);
		LayTableResponse<SaleQuotationViEn> po = new LayTableResponse<SaleQuotationViEn>(orderList);
		return po;
	}

	@Override
	public SaleQuotationViEn findSaleQuotationBy(Integer id) {
		return saleQuotationViEnMapper.selectOneBy(id);
	}

	@Transactional
	@Override
	public Response<String> edit(SaleQuotationEditEn saleQuotationEditEn,Integer userId) {
		SaleQuotation saleQuotation = saleQuotationMapper.selectByPrimaryKey(saleQuotationEditEn.getId());
		if (saleQuotation == null) {
			logger.error("编辑销售报价单，根据id:{}查询到的销售报价单为空", saleQuotationEditEn.getId());
			throw new RuntimeException("根据id查询到的销售报价单为空");
		}
		UtilsEntity.copyEntity(saleQuotationEditEn, saleQuotation);

		saleQuotation.setModifyTime(new Date());
		saleQuotation.setModifyId(userId);
		saleQuotationMapper.updateByPrimaryKey(saleQuotation);
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Transactional
	@Override
	public Response<SaleQuotationViEn> reivew(Integer userid, Integer id) {
		SaleQuotation record = saleQuotationMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("审核销售报价单，根据id:{}查询到的销售报价单为空", id);
			throw new RuntimeException("根据id查询到的销售报价单为空");
		}
		record.setReviewId(userid);
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleQuotationStatus.HAS_REVIEW_CODE));
		int count = saleQuotationMapper.updateByPrimaryKey(record);
		if (count == 1) {
			SaleQuotationViEn saleQuotationInfo = saleQuotationViEnMapper.selectOneBy(id);
			return new Response<SaleQuotationViEn>(ResultEnum.SUCCESS, saleQuotationInfo);
		} else {
			return new Response<SaleQuotationViEn>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<SaleQuotationViEn> cancelReview(Integer userid, Integer id) {
		SaleQuotation record = saleQuotationMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("弃审销售报价单，根据id:{}查询到的销售报价单为空", id);
			throw new RuntimeException("根据id查询到的销售报价单为空");
		}
		record.setModifyId(userid);
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleQuotationStatus.NOT_REVIEW_CODE));
		int count = saleQuotationMapper.updateByPrimaryKey(record);
		if (count == 1) {
			SaleQuotationViEn saleQuotationViEn = saleQuotationViEnMapper.selectOneBy(id);
			return new Response<SaleQuotationViEn>(ResultEnum.SUCCESS, saleQuotationViEn);
		} else {
			return new Response<SaleQuotationViEn>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<String> close(Integer id) {
		SaleQuotation record = saleQuotationMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("关闭销售报价单，根据id:{}查询到的销售报价单为空", id);
			throw new RuntimeException("根据id查询到的销售报价单为空");
		}
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleQuotationStatus.CLOSE_CODE));
		int count = saleQuotationMapper.updateByPrimaryKey(record);
		if (count == 1) {
			return new Response<String>(ResultEnum.SUCCESS);
		} else {
			return new Response<String>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<String> remove(Integer id) {
		int orderDelCount = saleQuotationMapper.deleteByPrimaryKey(id);
		if (orderDelCount != 1) {
			logger.error("关闭销售报价单，根据id:{}查询到的销售报价单为空", id);
			throw new RuntimeException("根据id查询到的销售报价单为空");
		}
		int count = saleQuotationDetailMapper.deleteByBillid(id);
		logger.debug("根据billid：{}删除销售报价单明细行数：{}", id, count);
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Transactional
	@Override
	public Response<String> add(SaleQuotationViEn saleQuotationViEn) {
		SaleQuotation record=new SaleQuotation();
		record.setBillNo(saleQuotationViEn.getBillNo());
		//根据BillNo查询销售报价单是已经有记录
		List<SaleQuotation> list = saleQuotationMapper.select(record);
		if(list.size()>0){
			logger.error("创建销售报价单，根据BillNo:{}查询到的销售报价单已经存在，不能创建", record.getBillNo());
			return new Response<>(500,"系统已有相同编号["+record.getBillNo()+"]的销售报价单,不能创建！请修改销售报价单编号。");
		}
		SaleQuotation saleQuotation = new SaleQuotation();
		// 复制相同字段值
		UtilsEntity.copyEntityValues(saleQuotationViEn, saleQuotation);
		// 创建时间
		saleQuotation.setCtime(new Date());
		// 单据状态
		saleQuotation.setReviewStatus(Integer.parseInt(SaleQuotationStatus.NOT_REVIEW_CODE));
		int i = saleQuotationMapper.insert(saleQuotation);
		if (i > 0) {
			return new Response<>(ResultEnum.SUCCESS);
		} else {
			return new Response<>(ResultEnum.ERROR);
		}
	}

	@Override
	public List<SaleQuotation> selectReviewOrderByCompanyId(SaleQuotation record) {
		return saleQuotationMapper.select(record);
	}

	@Override
	public List<TypeInfo> queryBillNo(Integer companyId) {
		return  saleQuotationViEnMapper.selectBillno(companyId);
	}

}
